<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once ('./application/controllers/authenticate.php');

class Banner extends Authenticate {

    const BANNER_PER_PAGE = 100;
    const BANNER_PAGE_RANGE = 5;

    function __construct() {
        global $active_layout_function;
        $active_layout_function = 'admin/site/banner/index';
        parent::__construct();
        $this->load->model('site_banner_model');
        $this->load->model('site_zone_model');
        $this->load->library('upload');
    }

    protected function _eventLoadLayout() {
        $this->page['title'] = 'Quản lý banner';
        $this->module['title'] = 'Quản lý banner';
    }

    function index() {
        try {
            $data = $this->_initData();
            $data['mode'] = 'grid';
            $this->setPageContent('admin/site/banner/grid', $data);
            $this->renderLayout('layouts/fullcolumn');
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

    protected function _initData() {
        global $frontend_pages;
        $data['pages'] = $frontend_pages;
        $data['zones'] = $this->site_zone_model->get_zones();
        $data['collection'] = $this->site_banner_model->get_collection(0, 0, null, 'bn.id DESC');
        $data['save_action'] = site_url('admin/site/banner/save', true);
        return $data;
    }

    function save() {
        try {
            $id = $this->input->post('id');
            $data = $this->input->post('data');
            $data['user_id'] = $this->admin_model->get_id();

            // Config Upload
            $config['allowed_types'] = 'gif|jpg|png|bmp|psd';
            $config['max_size'] = '500';
            $config['upload_path'] = './media/banner/';

            if (empty($id)) {
                if (isset($_FILES['data_file']) && $_FILES['data_file']['size'] > 0) {
                    $this->upload->initialize($config);
                    $this->upload->do_upload('data_file');

                    // Render Image Path
                    $data_upload = $this->upload->data();
                    $file_path = $config['upload_path'] . $data_upload['file_name'];
                    if (strlen($data_upload['file_name']) > 0) {
                        $data['file'] = $file_path;
                    }
                }
                $id = $this->site_banner_model->add_banner($data);
                $this->st_registry->register('admin/site/banner/success', 'Thêm mới banner thành công');
            } else {
                if (isset($_FILES['data_file']) && $_FILES['data_file']['size'] > 0) {
                    // Remove old file
                    $old_file = $this->site_banner_model->get_file($id);
                    @unlink($old_file);

                    $this->upload->initialize($config);
                    $this->upload->do_upload('data_file');

                    // Render Image Path
                    $data_upload = $this->upload->data();
                    $file_path = $config['upload_path'] . $data_upload['file_name'];
                    if (strlen($data_upload['file_name']) > 0) {
                        $data['file'] = $file_path;
                    }
                }
                $this->site_banner_model->update_banner($id, $data);
                $this->st_registry->register('admin/site/banner/success', 'Cập nhật thông tin banner thành công');
            }            
        } catch (Exception $ex) {
            $this->st_registry->register('admin/site/banner/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
        redirect(site_url('admin/site/banner/index', true));
    }

    function ajax_edit() {
        try {
            global $frontend_pages;
            $id = $this->input->getParam('id');
            if (empty($id)) {
                throw new Exception('Không tìm thấy banner');
                exit();
            }
            $data['banner'] = $this->site_banner_model->load_by_id($id);
            $data['pages'] = $frontend_pages;
            $data['zones'] = $this->site_zone_model->get_zones();
            $data['save_action'] = site_url('admin/site/banner/save', true);
            $data['mode'] = 'ajax_edit';
            $this->setPageContent('admin/site/banner/ajax/edit', $data);
            $this->renderLayout('layouts/ajax');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/site/banner/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
    }

    function edit() {
        try {
            $id = $this->input->getParam('id');
            if (empty($id)) {
                throw new Exception('Không tìm thấy banner');
                exit();
            }
            $data = $this->_initData();
            $data['banner'] = $this->site_banner_model->load_by_id($id);
            $data['parents'] = $this->site_banner_model->get_parent_banners($data['banner']->id);
            $data['mode'] = 'edit';
            $this->setPageContent('admin/site/banner/grid', $data);
            $this->renderLayout('layouts/fullcolumn');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/site/banner/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
    }

    function delete() {
        try {
            $id = $this->input->getParam('id');
            if (empty($id)) {
                $this->st_registry->register('admin/site/banner/error', 'Không tìm thấy banner (ID)?');
                redirect(site_url('admin/site/banner/index', true));
                exit();
            }
            // Remove old file
            $old_file = $this->site_banner_model->get_file($id);
            @unlink($old_file);
            $this->site_banner_model->delete_banner($id);
            $this->st_registry->register('admin/site/banner/success', 'Xóa banner thành công');
        } catch (Exception $ex) {
            $this->st_registry->register('admin/site/banner/error', 'Lỗi xảy ra: ' . $ex->getMessage());
        }
        redirect(site_url('admin/site/banner/index', true));
    }

}